package com.yunxin.admin.system.api.wechat;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunxin.core.wechat.domain.WechatFollowCustomer;
import com.yunxin.core.wechat.domain.WechatInfo;
import com.yunxin.core.wechat.dto.GetWechatCustomersDto;
import com.yunxin.core.wechat.javaxml.com.qq.weixin.mp.aes.AesException;
import com.yunxin.core.wechat.service.QyWxService;
import com.yunxin.core.wechat.service.WechatInfoService;
import com.yunxin.core.wechat.vo.WechatFollowCustomerListVo;
import com.yunxin.core.wechat.vo.WechatGroupTagVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author zero°
 * @since 2023-06-08
 */
@Tag(name = "01.微信回调接口")
@Slf4j
@RestController
@RequestMapping("/callback")
@RequiredArgsConstructor
public class WechatController {


    @Autowired
    private QyWxService qyWxService;
    @Autowired
    private WechatInfoService wechatInfoService;


//    /**
//     * 数据回调URL
//     * 3.1 支持Http Get请求验证URL有效性
//     * 3.2 支持Http Post请求接收业务数据
//     *
//     * @return
//     */
//    @Operation(summary = "数据回调URL")
//    @RequestMapping(value = "/data", method = {RequestMethod.GET, RequestMethod.POST})
//    //@PostMapping("/data")
//    public Object CompanyDataChangeNotice(HttpServletRequest request, @RequestBody(required = false) String body) throws AesException {
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        String jsonString = JSONObject.toJSONString(parameterMap);
//        log.info("企业微信回调参数：{},  解析参数：{}", jsonString, body);
//
//        if (body == null) {
//            Object result = qyWxService.verificationUrl(request,null);
//            return result;
//        }
//        Map<String, String> resultMap = qyWxService.getRequestParameter(request, body);
//        System.err.println(resultMap);
//        return "success";
//    }
//
//    /**
//     * 指令回调URL
//     * 3.1 支持Http Get请求验证URL有效性
//     * 3.2 支持Http Post请求接收业务数据
//     *
//     * @return
//     */
//    @Operation(summary = "指令回调URL")
//    @RequestMapping(value = "/command", method = {RequestMethod.GET, RequestMethod.POST})
//    //@PostMapping("/command")
//    public Object CompanyCommandChangeNotice(HttpServletRequest request, @RequestBody(required = false) String body) throws AesException {
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        String jsonString = JSONObject.toJSONString(parameterMap);
//        log.info("企业微信回调参数：{},  解析参数：{}", jsonString, body);
//        LambdaQueryWrapper<WechatInfo> wechatInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
//        wechatInfoLambdaQueryWrapper.eq(WechatInfo::getCorpid,"wwa759338446bc59af");
//        WechatInfo wechatInfo = wechatInfoService.getOne(wechatInfoLambdaQueryWrapper);
//        if (body == null) {
//            Object result = qyWxService.verificationUrl(request,null,wechatInfo);
//            return result;
//        }
//        Map<String, String> resultMap = qyWxService.getRequestParameter(request, body);
//        System.err.println(resultMap);
//        return "success";
//    }

    @Operation(summary = "指令回调URL")
    @RequestMapping(value = "/command/{corpid}", method = {RequestMethod.GET, RequestMethod.POST})
    public Object wwa759338446bc59af(HttpServletRequest request,
                                     @Parameter(description = "用户ID") @PathVariable String corpid,
                                     @RequestBody(required = false) String body) throws AesException {
        log.error("指令回调URL corpid:"+corpid);
        log.error("指令回调URL body:"+body);
        LambdaQueryWrapper<WechatInfo> wechatInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        request.getRequestURI();
        wechatInfoLambdaQueryWrapper.eq(WechatInfo::getCorpid,corpid);
        WechatInfo wechatInfo = wechatInfoService.getOne(wechatInfoLambdaQueryWrapper);
        if (ObjectUtils.isEmpty(wechatInfo)){
            return null;
        }
        Map<String, String[]> parameterMap = request.getParameterMap();
        String jsonString = JSONObject.toJSONString(parameterMap);
        log.info("企业微信回调参数：{},  解析参数：{}", jsonString, body);
        if (body == null) {
            Object result = qyWxService.verificationUrl(request,null,wechatInfo);
            return result;
        }
        Map<String, String> resultMap = qyWxService.getRequestParameter(request, body,wechatInfo);
        System.err.println(resultMap);
        return "success";
    }

//    @Operation(summary = "微信消息指令回调URL")
//    @RequestMapping(value = "/messageCommand", method = {RequestMethod.GET, RequestMethod.POST})
//    //@PostMapping("/command")
//    public Object messageCommand(HttpServletRequest request, @RequestBody(required = false) String body) throws AesException {
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        String jsonString = JSONObject.toJSONString(parameterMap);
//        log.info("企业微信回调参数：{},  解析参数：{}", jsonString, body);
//
//        if (body == null) {
//            Object result = qyWxService.verificationUrl(request,1);
//            return result;
//        }
//        //Map<String, String> resultMap = qyWxService.getRequestParameter(request, body);
//        //System.err.println(resultMap);
//        return "success";
//    }


//    //测试
//    @Operation(summary = "通过企业微信id获取token")
//    //@RequestMapping(value = "/getSuiteAccessToken", method = {RequestMethod.GET, RequestMethod.POST})
//    @GetMapping("/getCorpTokenByCorpId")
//    public Object getCorpTokenByCorpId(@Validated @RequestParam("corpId") String corpId) throws AesException {
//        String st = qyWxService.getCorpTokenByCorpId(corpId);
//        return st;
//    }
//    //测试
//    @Operation(summary = "通过企业微信id和手机号获取企微用户id")
//    @GetMapping("/getWechatUserByPhone")
//    public Object getWechatUserByPhone(@Validated @RequestParam("corpId") String corpId,@Validated @RequestParam("phone") String phone) throws AesException {
//        String wechat_user_id = qyWxService.getWechatUserByPhone(phone,corpId);
//        return wechat_user_id;
//    }
//
//    //测试
//    @Operation(summary = "通过企业微信邮箱获取企微用户id")
//    @GetMapping("/getWechatUserByEmail")
//    public Object getWechatUserByEmail(@Validated @RequestParam("corpId") String corpId
//            ,@Validated @RequestParam("email") String email
//            ,@Validated @RequestParam("emailType") Integer emailType) throws AesException {
//        String wechat_user_id = qyWxService.getWechatUserByEmail(email,corpId,emailType);
//        return wechat_user_id;
//    }
//
//    //测试
//    @Operation(summary = "通过企业微信id 获取所有标签")
//    @GetMapping("/getWechatTags")
//    public Object getWechatTags(@Validated @RequestParam("corpId") String corpId) throws AesException {
//        List<WechatGroupTagVo> wechatTags = qyWxService.getWechatTags(corpId);
//        return wechatTags;
//    }
//    //测试
//    @Operation(summary = "通过企业微信id用户id 获取所有客户信息")
//    @GetMapping("/getWechatCustomers")
//    public Object getWechatCustomers(@Validated @RequestParam("corpId") String corpId,@Validated @RequestParam("wechatUserId") String wechatUserId) throws AesException {
//        GetWechatCustomersDto getWechatCustomersDto=new GetWechatCustomersDto();
//        List<String> userid_list = new ArrayList<>();
//        userid_list.add(wechatUserId);
//        getWechatCustomersDto.setUserid_list(userid_list);
//        WechatFollowCustomerListVo wechatCustomers = qyWxService.getWechatCustomers(corpId, getWechatCustomersDto);
//        return wechatCustomers;
//    }
//    //测试
//    @Operation(summary = "通过企业微信id用户id客户id 获取客户信息")
//    @GetMapping("/getWechatCustomer")
//    public Object getWechatCustomer(@Validated @RequestParam("corpId") String corpId,
//                                    @Validated @RequestParam("externalUserid") String externalUserid,
//                                    @Validated @RequestParam("wechatUserId") String wechatUserId) throws AesException {
//        WechatFollowCustomer wechatCustomer = qyWxService.getWechatCustomer(corpId, externalUserid, wechatUserId);
//        return wechatCustomer;
//    }


}
